//
//  AppDelegate+ALJPushHelper.m
//  THB
//
//  Created by 吴泽楠 on 16/9/8.
//  Copyright © 2016年 阿拉丁电商. All rights reserved.
//

#import "AppDelegate+ALJPushHelper.h"
#import "ALJPushHelper.h"

#define JPushSDK_AppKey  @"713d3b209185a3dbd069dcce"
#define isProduction     NO

#define IOS8 ([[[UIDevice currentDevice] systemVersion] doubleValue] >=8.0 ? YES : NO)

@implementation AppDelegate (ALJPushHelper)

-(void)JPushApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    
    [ALJPushHelper setupWithOption:launchOptions appKey:JPushSDK_AppKey channel:nil apsForProduction:isProduction advertisingIdentifier:nil];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // Required - 注册 DeviceToken
    [ALJPushHelper registerDeviceToken:deviceToken];
}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required,For systems with less than or equal to iOS6
    [ALJPushHelper handleRemoteNotification:userInfo completion:nil];
}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    if (IOS8) { //iOS8以上包含iOS8
        if ([[UIApplication sharedApplication] currentUserNotificationSettings].types  == UIRemoteNotificationTypeNone) {
            
            NSLog(@"关闭");
            //提醒用户打开通知设置
            UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"请打开消息设置"
                                  message:@"应用有新信息通知您,请打开消息设置"
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  otherButtonTitles:@"确定",nil];
            alert.tag = 10001;
            [alert show];
            
        }else{
            NSLog(@"打开");
            [ALJPushHelper handleRemoteNotification:userInfo completion:completionHandler];
            
            // 应用正处理前台状态下，不会收到推送消息，因此在此处需要额外处理一下
            if (application.applicationState == UIApplicationStateActive) {
                UIAlertView *alert = [[UIAlertView alloc]
                                      initWithTitle:@"收到推送消息"
                                      message:userInfo[@"aps"][@"alert"]
                                      delegate:nil
                                      cancelButtonTitle:@"取消"
                                      otherButtonTitles:@"确定",nil];
                [alert show];
            }
            
        }
    }else{ // ios7 以下
        if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes]  == UIRemoteNotificationTypeNone) {
            NSLog(@"关闭");
            //提醒用户打开通知设置
            UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"请打开消息设置"
                                  message:@"应用有新信息通知您,请打开消息设置"
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  otherButtonTitles:@"确定",nil];
            alert.tag = 10001;
            [alert show];
        }else{
            NSLog(@"打开");
            
            [ALJPushHelper handleRemoteNotification:userInfo completion:completionHandler];
            // 应用正处理前台状态下，不会收到推送消息，因此在此处需要额外处理一下
            if (application.applicationState == UIApplicationStateActive) {
                UIAlertView *alert = [[UIAlertView alloc]
                                      initWithTitle:@"收到推送消息"
                                      message:userInfo[@"aps"][@"alert"]
                                      delegate:nil
                                      cancelButtonTitle:@"取消"
                                      otherButtonTitles:@"确定",nil];
                [alert show];
            }
        }
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 10001) {
        if (buttonIndex == 1) {
            //设置界面
            NSURL *url = [NSURL URLWithString:@"prefs:root=NOTIFICATIONS_ID"];
            if ([[UIApplication sharedApplication] canOpenURL:url])
            {
                [[UIApplication sharedApplication] openURL:url];
            }
            
        }
    }
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    [ALJPushHelper showLocalNotificationAtFront:notification];
    return;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [application setApplicationIconBadgeNumber:0];
    return;
}

@end
